//
//  ZYCustomTabbar.swift
//  SwiftDemo1
//
//  Created by wangzhipan on 2025/5/30.
//

import Foundation
import UIKit
import SnapKit

protocol IZYTabbarItem {
    var normalTitle: String {get}
    var normalTitleColor: UIColor {get}
    var normalImage: UIImage? {get}
    
    var selectTitle: String? {get}
    var selectTitleColor: UIColor? {get}
    var selectImage: UIImage? {get}
    
    var badgeValue: String? {get}
}


@objc protocol ZYCustomTabbarDelegate: AnyObject {
    @objc optional func customTabbar(_ tabbar: ZYCustomTabbar, didSelectIndex index:Int)
    
    @objc optional func addButtonTapped()
}

class ZYCustomTabbar: UIView {
    static let kOriginTag: Int = 99999
    
    weak var delegate: ZYCustomTabbarDelegate?
    private(set) var selectedIndex: Int = 0
    
    private var stackView: UIStackView = UIStackView()
    private var btns: [UIButton] = []
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
        layoutUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        backgroundColor = .white
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        addSubview(stackView)
    }
    
    private func layoutUI() {
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
        }
        stackView.backgroundColor = .yellow
    }
    
    func setupTabbar(_ models: [IZYTabbarItem]) {
        btns.forEach({
            $0.removeFromSuperview()
        })
        let midIndex = models.count / 2
        for index in 0..<models.count {
            if (index == midIndex) {
                addComposeBtn()
            }
            let item = models[index]
            let btn = ZYImageTopButton(type: .custom)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.updateUI(forItem: item)
            btn.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            btn.tag = ZYCustomTabbar.kOriginTag + index
            btns.append(btn)
            stackView.addArrangedSubview(btn)
        }
        
        updateSelectedIndex(0)
    }
    
    private func addComposeBtn() {
        let componseBtn = UIButton(type: .custom)
        componseBtn.adjustsImageWhenDisabled = false
        componseBtn.adjustsImageWhenHighlighted = false
        componseBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        componseBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        componseBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        componseBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        componseBtn.addTarget(self, action: #selector(componseBtnTapped), for: .touchUpInside)
        componseBtn.sizeToFit()
        let containView = UIView()
        containView.addSubview(componseBtn)
        componseBtn.frame.origin.y = 5
        stackView.addArrangedSubview(containView)
    }
    
    private func updateSelectedIndex(_ index: Int) {
        var tmpIndex = index
        if tmpIndex >= btns.count {
            tmpIndex = 0
        }
        if tmpIndex >= btns.count {
            return
        }
        selectedIndex = tmpIndex
        let btn = btns[tmpIndex]
        btn.isSelected = true
        
        if let delegate = self.delegate {
            delegate.customTabbar?(self, didSelectIndex: selectedIndex)
        }
    }
}

// MARK: -事件监听
extension ZYCustomTabbar {
    @objc private func componseBtnTapped() {
        if let delegate = self.delegate {
            delegate.addButtonTapped?()
        }
    }
    
    @objc private func buttonTapped(_ sender: ZYImageTopButton) {
        let lastSenderTag = selectedIndex + ZYCustomTabbar.kOriginTag
        let lastSender = stackView.viewWithTag(lastSenderTag)
        if let lastSender = lastSender as? UIButton {
            lastSender.isSelected = false
        }
        
        selectedIndex = sender.tag - ZYCustomTabbar.kOriginTag
        sender.isSelected = true
        if let delegate = self.delegate {
            delegate.customTabbar?(self, didSelectIndex: selectedIndex)
        }
    }
}

